<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-card class="search-card">
      <el-form :model="queryParams" ref="queryParams" size="small" label-width="88px">
        <el-row :gutter="0">
          <el-col :span="5">
            <el-form-item label="供应商名称" prop="supplierName">
              <el-input
                placeholder="请输入供应商名称"
                v-model="queryParams.supplierName"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="使用状态" prop="isOpen">
              <el-select
                v-model="queryParams.isOpen"
                placeholder="请选择使用状态"
                @keyup.enter.native="handleQuery"
                style="width: 100%"
              >
                <el-option value="1" label="开启"></el-option>
                <el-option value="2" label="关闭"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="14">
            <el-form-item>
              <div class="dis aic">
                <div class="flex_1"></div>
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  size="mini"
                  @click="handleQuery"
                >
                  搜索
                </el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
                  重置
                </el-button>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card class="search-card">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="buttonMethods(1)"
            v-hasPermi="['teamMeal:supplier:add']"
          >
            添加
          </el-button>
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
    </el-card>
    <el-card class="table-card">
      <el-table :data="dataList">
        <el-table-column label="供应商名称" align="center" prop="supplierName" />
        <el-table-column label="开户行" align="center" prop="bankName" />
        <el-table-column label="开户账号" align="center" prop="accountNumber" />
        <el-table-column label="商户编号" align="center" prop="merId" />
        <el-table-column label="使用状态" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.isOpen == 1">开启</el-tag>
            <el-tag v-if="scope.row.isOpen == 2" type="danger">关闭</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" />
        <el-table-column label="操作" align="center" width="200px">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="mini"
              @click="buttonMethods(1, scope.row)"
              v-hasPermi="['teamMeal:supplier:edit']"
              >修改</el-button
            >
            <el-button
              type="text"
              size="mini"
              @click="buttonMethods(5, scope.row)"
              v-hasPermi="['teamMeal:supplier:status']"
            >
              <span v-if="scope.row.isOpen == 1">关闭</span>
              <span v-if="scope.row.isOpen == 2">开启</span>
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body top="2vh">
      <el-form ref="form" :model="queryForm" label-width="180px" size="small">
        <el-row>
          <el-col :span="20">
            <el-form-item label="供应商名称" class="is-required">
              <el-input
                v-model="queryForm.supplierName"
                placeholder="请输入供应商名称"
                maxlength="50"
                show-word-limit
              />
            </el-form-item>
            <el-form-item label="开户行" class="is-required">
              <el-input
                v-model="queryForm.bankName"
                placeholder="请输入开户行"
                maxlength="50"
                show-word-limit
              />
            </el-form-item>
            <el-form-item label="账户号" class="is-required">
              <el-input
                v-model="queryForm.accountNumber"
                placeholder="请输入账户号"
                maxlength="50"
                show-word-limit
              />
            </el-form-item>
            <el-form-item label="商编" class="is-required">
              <el-input
                v-model="queryForm.merId"
                placeholder="请输入商编"
                maxlength="50"
                show-word-limit
              />
            </el-form-item>
            <el-form-item label="商户通道编号" class="is-required">
              <el-input
                v-model="queryForm.merchantCode"
                placeholder="请输入商户通道编号"
                maxlength="50"
                show-word-limit
              />
            </el-form-item>

            <el-form-item label="法人名称" class="is-required">
              <el-input
                v-model="queryForm.legalName"
                placeholder="请输入法人名称"
                maxlength="50"
                show-word-limit
              />
            </el-form-item>
            <el-form-item label="联系电话" class="is-required">
              <el-input
                v-model="queryForm.contactPhone"
                placeholder="请输入联系电话"
                maxlength="11"
                show-word-limit
              />
            </el-form-item>
            <el-form-item label="上传营业执照" class="is-required">
              <image-upload
                v-model="queryForm.businessLicenseUrl"
                :limit="1"
              ></image-upload>
            </el-form-item>
            <el-form-item label="食品经营许可证" class="is-required">
              <image-upload v-model="queryForm.foodUrl" :limit="1"></image-upload>
            </el-form-item>
            <el-form-item label="是否启用" class="is-required">
              <el-radio-group v-model="queryForm.isOpen">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="2">否</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="内容描述">
              <el-input
                type="textarea"
                v-model="queryForm.remarkDetail"
                placeholder="请输入内容描述"
                maxlength="200"
                show-word-limit
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="buttonMethods(2)">确 定</el-button>
        <el-button @click="buttonMethods(3)">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { insert, update, queryByPage } from "@/api/teamMeal/supplier";

export default {
  name: "studentsupplier",
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 1,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      dataList: [],
      campusList: [],
      gradeList: [],
      queryForm: {},
    };
  },
  created() {
    this.getList();
  },
  activated() {
    this.getList();
  },
  methods: {
    // 列表
    getList() {
      queryByPage(this.queryParams).then((res) => {
        if (res.code == 200) {
          this.dataList = res.data.rows;
          this.total = res.data.total;
        }
      });
    },
    /**
     * 按钮操作的方法
     *
     * @param options 操作选项，用于区分不同的按钮操作
     * @param value 按钮值
     */
    buttonMethods(options, value) {
      // 添加 修改
      var that = this;
      if (options == 1) {
        if (value) {
          this.title = "修改供应商";
          this.queryForm = { ...value };
          this.open = true;
        } else {
          this.title = "添加供应商";
          this.queryForm = {
            isOpen: 1,
          };
          this.open = true;
        }
      }
      // 提交
      else if (options == 2) {
        var data = { ...this.queryForm };
        console.log(data);
        if (!data.supplierName) {
          return this.$message.error("请输入供应商名称");
        }
        if (!data.bankName) {
          return this.$message.error("请输入开户行名称");
        }
        if (!data.accountNumber) {
          return this.$message.error("请输入账户号");
        }
        if (!data.merId) {
          return this.$message.error("请输入商编");
        }
        if (!data.merchantCode) {
          return this.$message.error("请输入商户通道编号");
        }
        if (!data.legalName) {
          return this.$message.error("请输入法人名称");
        }
        if (!data.contactPhone) {
          return this.$message.error("请输入联系电话");
        }
        if (!data.businessLicenseUrl) {
          return this.$message.error("请上传营业执照照片");
        }
        if (!data.foodUrl) {
          return this.$message.error("请上传食品营业许可证照片");
        }

        if (data.id) {
          update(data).then((res) => {
            if (res.code == 200) {
              that.getList();
              that.open = false;
              that.$modal.msgSuccess("修改成功");
            } else {
              that.$modal.msgError("修改失败");
            }
          });
        }
        // 添加餐别信息
        else {
          insert(data).then((res) => {
            if (res.code == 200) {
              that.getList();
              that.open = false;
              that.$modal.msgSuccess("添加成功");
            } else {
              that.$modal.msgError("添加失败");
            }
          });
        }
      }
      // 关闭弹框
      else if (options == 3) {
        this.title = "";
        this.queryForm = {};
        this.open = false;
      }
      // 关闭 开启
      else if (options == 5) {
        var str = value.status == 1 ? "关闭" : "开启";
        this.$confirm("此操作将" + str + "餐别, 是否继续?", str, {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            update({ id: value.id, isOpen: value.isOpen == 1 ? 2 : 1 }).then((res) => {
              if (res.code == 200) {
                that.getList();
                that.open = false;
                that.$modal.msgSuccess("操作成功");
              } else {
                that.$modal.msgError("操作失败");
              }
            });
          })
          .catch(() => {});
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryParams");
      this.handleQuery();
    },
  },
};
</script>
<style>
.padding_4_10 {
  padding: 4px 10px !important;
}
.el-upload-list--picture-card .el-upload-list__item {
  width: 90px !important;
  height: 90px !important;
  line-height: 90px !important;
}
.el-upload--picture-card {
  width: 90px !important;
  height: 90px !important;
  line-height: 90px !important;
}
</style>
